$(() => {

    // 尝试从localStorage获取用户信息
    let loginUser = localStorage.getItem('userInfo');
    reloadMember();

    $('.login').click(function () {
        window.location.href = 'http://localhost:8080/client/login';
    });


    function reloadMember() {

        if (loginUser != null) {
            // 刷新界面
            loginUser = JSON.parse(loginUser);
            $('.infos .id .info').text(loginUser.patId)
            $('.infos .phone .info').text(loginUser.phone)
            $('.infos .name .info').text(loginUser.name)
            $('.infos .balance .info').text(loginUser.price)
            $('.login').hide();
            $('.change').show();
            $('.logout').show();
        } else {
            $('.infos .id .info').text('未登录')
            $('.infos .phone .info').text('未登录')
            $('.infos .name .info').text('未登录')
            $('.infos .balance .info').text('未登录')
            $('.login').show();
            $('.change').hide();
            $('.logout').hide();
        }
    }


    $('.change').click(function () {
        sessionStorage.removeItem('userInfo');
        loginUser = null;
        window.location.href = 'http://localhost:8080/client/login';
    });

    $('.logout').click(function () {
        // 发送 AJAX 请求到服务器端的登出端点
        $.ajax({
            url: ctx + "/client/logout",
            method: 'get',
            dataType: 'json',
            success: function(response) {
                // 请求成功，处理响应
                console.log('登出成功');
            },
            complete: function() { // 注意这里的冒号
                // 清除前端存储的用户信息
                loginUser = null;
                localStorage.removeItem('userInfo');
                sessionStorage.removeItem('userInfo');
                window.location.href = 'http://localhost:8080/client/login';
                reloadMember();
            }
        });
    });
});

